# Python 2.6.4
# Project Euler, Problem 59
# Copyright 2010 Talha Zaman

def decrypt(ciphertext, key):
    plaintext = ""
    for i in range(len(ciphertext)):
        letter = ciphertext[i] ^ ord(key[i%3])
        plaintext+=chr(letter)
    return plaintext

text = [int(c) for c in open("cipher1.txt").readline().strip().split(",")]
candidates=[]
keys=[]
for i in range(97,123):
    for j in range(97,123):
        for k in range(97,123):
            key = chr(i)+chr(j)+chr(k)
            plain = decrypt(text, key)
            if plain.find("the") != -1 and plain.find("and") != 1:
                candidates.append(plain)
                keys.append(key)

x = [sum([1 for c in candidates[a] if ord(c) in range(65,123)]) for a in range(len(candidates))]
print candidates[x.index(max(x))]
print "Key: ", keys[x.index(max(x))]
print "Answer: ", sum([ord(c) for c in ans])
